package org.mathcloud.math.core.scilab.factory;

import org.mathcloud.math.core.common.factory.QuantityObjectFactory;
import org.mathcloud.math.core.common.quantity.ComplexMatrix;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.MatrixObject;
import org.mathcloud.math.core.common.quantity.QuantityObject;
import org.mathcloud.math.core.common.quantity.RealMatrix;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;
import org.mathcloud.math.core.scilab.quantity.ScilabComplexMatrix;
import org.mathcloud.math.core.scilab.quantity.ScilabComplexNumber;
import org.mathcloud.math.core.scilab.quantity.ScilabQuantityObject;
import org.mathcloud.math.core.scilab.quantity.ScilabRealMatrix;
import org.mathcloud.math.core.scilab.quantity.ScilabRealNumber;
import org.scilab.modules.types.ScilabDouble;

public class ScilabQuantityObjectFactory implements QuantityObjectFactory
{
	public static ScilabQuantityObject getInstance(QuantityObject in)
	{		
		if (in==null)
			return null;
		if (in instanceof ScilabQuantityObject)
			return (ScilabQuantityObject)in;
		else if (in.getSignature().isRealNumberSignature())
		{
			return new ScilabRealNumber(((RealNumber)in).getValue());
		}
		else if (in.getSignature().isComplexNumberSignature())
		{			
			return new ScilabComplexNumber(((ComplexNumber)in).getRealPart(),
					((ComplexNumber)in).getImagePart());
		}
		else if (in.getSignature().isRealMatrixSignature())
		{
			return new ScilabRealMatrix((RealMatrix)in);
		}
		else if (in.getSignature().isComplexMatrixSignature())
		{
			return new ScilabComplexMatrix((ComplexMatrix)in);
		}
		else return null;
	}
	public static ScilabQuantityObject getInstance(ScilabDouble sciObj)
	{
		if (sciObj==null)
			return null;
		else if (sciObj.isEmpty())
			return null;
		else 
		{
			ScilabQuantityObject res = null;
			if ((sciObj.getHeight()==1)&&(sciObj.getWidth()==1))
			{
				if (sciObj.isReal())
				{
					res = new ScilabRealNumber();
					res.setScilabObject(sciObj);
					return res;
				}
				else
				{
					res = new ScilabComplexNumber();
					res.setScilabObject(sciObj);
					return res;					
				}				
			}
			else
			{
				if (sciObj.isReal())
				{
					res = new ScilabRealMatrix(sciObj.getHeight(),sciObj.getWidth());
					res.setScilabObject(sciObj);
					return res;
				}
				else
				{
					res = new ScilabComplexMatrix(sciObj.getHeight(),sciObj.getWidth());
					res.setScilabObject(sciObj);
					return res;					
				}								
			}
		}
	}
	
	@Override
	public QuantityObject newInstance(QuantityObjectSignature arg0)
	{
		if (arg0.isRealNumberSignature())
			return new ScilabRealNumber();
		else if (arg0.isComplexNumberSignature())
			return new ScilabComplexNumber();
		else if (arg0.isRealMatrixSignature())
			return new ScilabRealMatrix(((MatrixObject)arg0).getNumberOfRows(),((MatrixObject)arg0).getNumberOfCols());
		else if (arg0.isComplexNumberSignature())
			return new ScilabComplexMatrix(((MatrixObject)arg0).getNumberOfRows(),((MatrixObject)arg0).getNumberOfCols());
		else return null;		
	}
		
}
